package com.alibaba.pet.controller;

import com.alibaba.basic.exception.BusinessException;
import com.alibaba.pet.service.ISearchMasterMsgService;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.query.SearchMasterMsgQuery;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg findById(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接口：发布寻主消息
     *
     * @param
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
           return searchMasterMsgService.publish(searchMasterMsg, request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }

    }
    /**
     * 接口：user用户查询寻主消息状态
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }

    /**
     * 接口：店铺管理员查询寻主消息状态待处理state = 1
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandel(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toHandel(query,request);
    }
    /**
     * 接口：店铺管理员查询寻主消息state = 3
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/poll")
    public PageList<SearchMasterMsg> poll(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.poll(query,request);
    }
    /**
     * 接口：店铺管理员查询寻主消息状态已处理 state = 2
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.finish(query,request);
    }
    // 拒单
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id")Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("拒单失败！");
        }
    }
    // 接单
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody Map<String,String> map){
        try {
            searchMasterMsgService.accept(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }
}